package C022;

import java.util.Arrays;
import java.util.Scanner;

/**
 * ClassName: Main1
 * Package: C022
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/7 00:00
 * @Version 1.0
 * @Since 1.0
 */
public class Main1 {
    public static void main(String[] args) {
//        int[] arr = {1, 3, 5, 2, 4, 6};
//        System.out.println(process(arr, 0, arr.length - 1));

//        Scanner scan = new Scanner(System.in);
//        int n = scan.nextInt();
//        int[] arr = new int[n];
//        for (int i = 0; i < n; i++) {
//            arr[i] = scan.nextInt();
//        }
//        System.out.println(process(arr, 0, arr.length - 1));

        int num = 1000;
        int size = 20;
        int length = 10;
        for (int i = 0; i < num; i++) {
            int[] arr1 = generateRandomArr(size, length);
            int[] arr2 = Arrays.copyOf(arr1, arr1.length);
            long i1 = process(arr1, 0, arr1.length - 1);
            long i2 = process2(arr2, 0, arr1.length - 1);
            if(i1 != i2) {
                System.out.println("error !!!!");
                System.out.println(Arrays.toString(arr1));
                System.out.println(Arrays.toString(arr2));
                System.out.println(i1);
                System.out.println(i2);
                return;
            }
        }
        System.out.println("success !!!!");
    }


    public static int[] generateRandomArr(int n, int size) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (int) (Math.random() * size);
        }
        return arr;
    }

    public static int process2(int[] arr, int l, int r) {
        if (l == r) {
            return 0;
        }
        int res = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] <= arr[j]) {
                    res += arr[i];
                }
            }
        }
        return res;
    }

    public static long process(int[] arr, int l, int r) {
        if (l == r) {
            return 0;
        }
        int m = l + ((r - l) >> 1);
        return process(arr, l, m) + process(arr, m + 1, r) + merge(arr, l, m, r);
    }

    private static long merge(int[] arr, int l, int m, int r) {
        long res = 0;
        long curAns = 0;
        int a = l;
        int b = m + 1;
        while (b <= r) {
            while (a <= m && arr[a] <= arr[b]) {
                curAns += arr[a++];
            }
            b++;
            res += curAns;
        }

        // 正常归并
        a = l;
        b = m + 1;
        int[] help = new int[r - l + 1]; // 复杂数组
        int index = 0;
        while (a <= m && b <= r) {
            if (arr[a] <= arr[b]) {
                help[index++] = arr[a++];
            } else {
                help[index++] = arr[b++];
            }
        }
        while (a <= m) {
            help[index++] = arr[a++];
        }
        while (b <= r) {
            help[index++] = arr[b++];
        }
        for (int i = 0; i < help.length; i++) {
            arr[l + i] = help[i];
        }
        return res;
    }
}
